@use 'sass:map';

@use './shared' as *;
@use './design' as *;

$radio: () !default;
$radio: map.merge(
  (
    color-checked-button: #fff,
    bg-color-button: transparent,
    bg-color-checked-button: get-css-var('color-primary-base'),
    bg-color-disabled-button: get-css-var('fill-color-background'),
    b-color: get-css-var('border-color-base'),
    b-color-hover: get-css-var('color-primary-base'),
    b-color-checked: get-css-var('color-primary-base'),
    b-color-disabled: get-css-var('fill-color-disabled'),
    signal-bg-color: transparent,
    signal-bg-color-checked: get-css-var('color-primary-base'),
    signal-bg-color-disabled: get-css-var('fill-color-disabled'),
    signal-b-color-hover: get-css-var('radio-b-color-hover'),
    label-color: inherit,
    label-color-hover-button: get-css-var('radio-b-color-hover'),
    label-color-checked: get-css-var('color-primary-base'),
    label-color-checked-button: #fff,
    label-color-disabled: get-css-var('content-color-disabled'),
    radius: get-css-var('radius-base'),
    s-color-focus: get-css-var('color-primary-opacity-6'),
    shadow-focus: get-css-var('shadow-focus') get-css-var('radio-s-color-focus'),
    signal-size: 18px,
    signal-inner-size: 10px,
    button-height: 32px,
    button-v-padding: 0,
    button-h-padding: 10px,
    series-span: 10px
  ),
  $radio
);

.#{$namespace}-radio {
  &-vars {
    @include define-preset-values('radio', $radio);
  }

  @mixin define-radio-style($style-map) {
    @include define-preset-style('radio', $style-map);
  }

  @include basis;

  position: relative;
  display: inline-flex;
  align-items: center;
  margin-inline-end: get-css-var('radio-series-span');
  vertical-align: middle;
  cursor: pointer;
  user-select: none;
  transition: get-css-var('transition-background'), get-css-var('transition-border'),
    get-css-var('transition-shadow');

  &,
  &--inherit {
    line-height: 1;
  }

  &:last-child {
    margin-inline-end: 0;
  }

  @mixin border-base-style {
    height: get-css-var('radio-button-height');
    padding: get-css-var('radio-button-v-padding') get-css-var('radio-button-h-padding');
    border: get-css-var('border-shape') get-css-var('radio-b-color');
    border-radius: get-css-var('radio-radius');

    &:hover {
      border-color: get-css-var('radio-b-color-hover');
    }
  }

  &--border {
    @include border-base-style;
  }

  &--checked#{&}--border {
    border-color: get-css-var('radio-b-color-checked');
  }

  &--disabled#{&}--border {
    &,
    &:hover {
      border-color: get-css-var('radio-b-color-disabled');
    }
  }

  @mixin button-base-style {
    @include border-base-style;

    background-color: get-css-var('radio-bg-color-button');
  }

  &--button {
    @include button-base-style;

    &:focus-within {
      border-color: get-css-var('radio-b-color-hover');
      box-shadow: get-css-var('radio-shadow-focus');
    }
  }

  &--checked#{&}--button {
    background-color: get-css-var('radio-bg-color-checked-button');
    border-color: get-css-var('radio-b-color-checked');
  }

  &--disabled#{&}--button {
    &,
    &:hover {
      background-color: get-css-var('radio-bg-color-disabled-button');
      border-color: get-css-var('radio-b-color-disabled');
    }
  }

  &--loading {
    cursor: auto;
  }

  &__signal {
    @include round-border(get-css-var('radio-b-color'));

    position: relative;
    display: inline-block;
    width: get-css-var('radio-signal-size');
    height: get-css-var('radio-signal-size');
    margin-inline-end: 6px;
    background-color: get-css-var('radio-signal-bg-color');
    border: get-css-var('border-shape') get-css-var('radio-b-color');
    border-radius: 50%;
    transition: get-css-var('transition-border'), get-css-var('transition-shadow');

    &::before {
      opacity: 0%;
    }

    &--active::before {
      opacity: 100%;
    }

    &::after {
      position: absolute;
      top: 50%;
      left: 50%;
      width: get-css-var('radio-signal-inner-size');
      height: get-css-var('radio-signal-inner-size');
      content: '';
      background-color: get-css-var('radio-signal-bg-color-checked');
      border-radius: 50%;
      opacity: 0%;
      transition: get-css-var('transition-background'), get-css-var('transition-opacity'),
        get-css-var('transition-transform');
      transform: translate(-50%, -50%) scale(0);
    }
  }

  &:focus-within &__signal {
    box-shadow: get-css-var('radio-shadow-focus');
  }

  &--button &__signal {
    display: none;
  }

  @mixin small-vars {
    @include define-preset-values(
      'radio',
      (
        signal-size: 14px,
        signal-inner-size: 7px,
        button-height: 24px,
        button-h-padding: 8px
      )
    );
  }

  @mixin large-vars {
    @include define-preset-values(
      'radio',
      (
        signal-size: 22px,
        signal-inner-size: 13px,
        button-height: 40px,
        button-h-padding: 14px
      )
    );
  }

  &--small {
    @include small-vars;
  }

  &--large {
    @include large-vars;
  }

  &:hover &__signal {
    &,
    &::before {
      border-color: get-css-var('radio-signal-b-color-hover');
    }
  }

  &--checked &__signal {
    &,
    &::before {
      border-color: get-css-var('radio-b-color-checked');
    }

    &::after {
      opacity: 100%;
      transform: translate(-50%, -50%) scale(1);
    }
  }

  &__label {
    display: inline-flex;
    align-items: center;
    color: get-css-var('radio-label-color');
    white-space: nowrap;
    transition: get-css-var('transition-color');
  }

  &--button:hover &__label,
  &--button:focus-within &__label {
    color: get-css-var('radio-label-color-hover-button');
  }

  &--checked &__label {
    color: get-css-var('radio-label-color-checked');
  }

  &--checked#{&}--button &__label {
    color: get-css-var('radio-label-color-checked-button');
  }

  &__loading {
    display: flex;
    align-items: center;
    height: 100%;
    margin-inline-end: 4px;
    pointer-events: none;
    user-select: none;
  }

  $states: success, error, warning;

  @each $state in $states {
    &--#{$state} {
      @include define-radio-style(
        (
          bg-color-checked-button: 'color' $state 'base',
          b-color: 'color' $state 'base',
          b-color-hover: 'color' $state 'base',
          b-color-checked: 'color' $state 'base',
          signal-bg-color-checked: 'color' $state 'base',
          label-color-checked: 'color' $state 'base',
          s-color-focus: 'color' $state 'opacity-6'
        )
      );
    }
  }

  $label: #{&}__label;

  &--disabled {
    cursor: not-allowed;

    &,
    &:hover {
      border-color: get-css-var('radio-b-color-disabled');
    }

    &,
    &:hover,
    &:focus-within {
      #{$label} {
        color: get-css-var('radio-label-color-disabled');
      }
    }
  }

  &--disabled &__signal,
  &--disabled:hover &__signal {
    &,
    &::before {
      border-color: get-css-var('radio-b-color-disabled');
    }

    &::after {
      background-color: get-css-var('radio-signal-bg-color-disabled');
    }
  }

  &__input {
    width: 0;
    height: 0;
    margin: 0;
    pointer-events: none;
    opacity: 0%;
  }

  @at-root {
    &-group {
      @include basis;

      display: inline-flex;
      align-items: center;
      justify-content: flex-start;

      &--vertical {
        flex-direction: column;
        align-items: flex-start;
        justify-content: center;
      }
    }

    &-group--vertical & {
      margin-inline-end: 0;
      margin-bottom: 6px;

      &:last-child {
        margin-bottom: 0;
      }
    }

    $radius: get-css-var('radius-base');
    $bw: get-css-var('border-width');
    $rbw: calc(get-css-var('border-width') * -1);

    &-group--button-group & {
      position: relative;
      z-index: 0;
      height: get-css-var('radio-button-height');
      padding: get-css-var('radio-button-v-padding') get-css-var('radio-button-h-padding');
      margin: 0;
      border: get-css-var('border-shape') get-css-var('radio-b-color');
      border-inline-start-width: 0;
      border-radius: 0;

      &::after {
        position: absolute;
        inset-inline-start: $rbw;
        top: 0;
        bottom: 0;
        display: block;
        width: $bw;
        content: '';
        background-color: transparent;
        transition: get-css-var('transition-background');
      }

      &:hover {
        border-color: get-css-var('radio-b-color-checked');
        box-shadow: $rbw 0 0 0 get-css-var('radio-b-color-checked');

        @include rtl {
          box-shadow: $bw 0 0 0 get-css-var('radio-b-color-checked');
        }
      }

      &:hover #{$label},
      &:focus-within #{$label} {
        color: get-css-var('radio-label-color-hover-button');
      }

      &:first-child {
        @include logical-radius($radius 0 0 $radius);

        border-inline-start-width: $bw;
        box-shadow: none;

        @include rtl {
          box-shadow: none;
        }

        &::after {
          content: none;
        }
      }

      &:last-child {
        @include logical-radius(0 $radius $radius 0);
      }

      &--checked {
        z-index: 1;
        border-color: get-css-var('radio-b-color-checked');
        box-shadow: $rbw 0 0 0 get-css-var('radio-b-color-checked');

        @include rtl {
          box-shadow: $bw 0 0 0 get-css-var('radio-b-color-checked');
        }
      }

      &:focus-within {
        border-color: get-css-var('radio-b-color-hover');
        box-shadow:
          $rbw 0 0 0 get-css-var('radio-b-color-hover'),
          get-css-var('radio-shadow-focus');

        @include rtl {
          box-shadow:
            $bw 0 0 0 get-css-var('radio-b-color-hover'),
            get-css-var('radio-shadow-focus');
        }

        &::after {
          inset-inline-start: -3px;
          top: -3px;
          bottom: -3px;
          background-color: get-css-var('radio-s-color-focus');
          opacity: 50%;
        }
      }

      &:focus-within:first-child {
        box-shadow: get-css-var('radio-shadow-focus');

        @include rtl {
          box-shadow: get-css-var('radio-shadow-focus');
        }
      }

      &__signal {
        display: none;
      }
    }

    &-group--vertical#{&}-group--button-group & {
      border-inline-start-width: $bw;
      border-top-width: 0;

      &::after {
        inset: $rbw 0 auto;
        width: auto;
        height: $bw;
      }

      &:hover {
        box-shadow: 0 $rbw 0 0 get-css-var('radio-b-color-checked');

        @include rtl {
          box-shadow: 0 $bw 0 0 get-css-var('radio-b-color-checked');
        }
      }

      &:first-child {
        @include logical-radius($radius $radius 0 0);

        border-top-width: $bw;
        box-shadow: none;

        @include rtl {
          box-shadow: none;
        }

        &::after {
          content: none;
        }
      }

      &:last-child {
        @include logical-radius(0 0 $radius $radius);
      }

      &--checked {
        box-shadow: 0 $rbw 0 0 get-css-var('radio-b-color-checked');

        @include rtl {
          box-shadow: 0 $bw 0 0 get-css-var('radio-b-color-checked');
        }
      }

      &:focus-within {
        box-shadow:
          0 $rbw 0 0 get-css-var('radio-b-color-hover'),
          get-css-var('radio-shadow-focus');

        @include rtl {
          box-shadow:
            0 $bw 0 0 get-css-var('radio-b-color-hover'),
            get-css-var('radio-shadow-focus');
        }

        &::after {
          inset-inline: -3px;
          top: -3px;
          bottom: auto;
        }
      }

      &:focus-within:first-child {
        box-shadow: get-css-var('radio-shadow-focus');

        @include rtl {
          box-shadow: get-css-var('radio-shadow-focus');
        }
      }
    }

    &-group--small & {
      @include small-vars;
    }

    &-group--large & {
      @include large-vars;
    }

    &-group--button-group#{&}-group--disabled &,
    &-group--button-group &--disabled {
      background-color: get-css-var('radio-bg-color-disabled-button');
      border-color: get-css-var('radio-b-color') !important;
      box-shadow: none !important;

      &,
      &:hover,
      &:focus-within {
        #{$label} {
          color: get-css-var('radio-label-color-disabled');
        }
      }
    }

    &-group--button-group &--disabled#{&}--checked,
    &-group--button-group#{&}-group--disabled &--checked {
      background-color: get-css-var('radio-bg-color-disabled-button');

      #{$label} {
        color: get-css-var('radio-label-color-disabled');
      }
    }
  }
}
